<?php
class Member extends CI_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->model('member_model');
//		$this->load->library('session');
	
	}
	
	/**
	 * 登录
	 * Enter description here ...
	 */
	public function login(){
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username', '用户名 ', 'required');
		$this->form_validation->set_rules('password', '密码', 'required');
		

		$data = array();
		$data['js'] = array('member_login');
		if($this->form_validation->run() == FALSE){
			$this->load->view('/templates/header', $data);
			$this->load->view('/member/login');
			$this->load->view('/templates/footer');
		}else{
			//登录。写入session
			
			$row_user = $this->member_model->login($this->input->post('username'), $this->input->post('password'));
//			print_r($row_user);
			if(count($row_user)>0){
				
				$userdata = array(
						'uid'	=>$row_user['id'],
						'username'=>$row_user['username']
				);
				$this->session->set_userdata($userdata);				
				
				$this->load->view('templates/header', $data);
				$this->load->view('templates/success', array('successinfo'=>'登录成功', 'url'=>'/'));
				$this->load->view('templates/footer');
//				print_r($this->session->all_userdata());
			}else{
				$data['login_error'] = '用户名或密码错误';
				$this->load->view('/templates/header', $data);
				$this->load->view('/member/login');
				$this->load->view('/templates/footer');
			}
			
			
		}
		
	}
	
	/**
	 * 退出
	 */
	public function logout(){
		$this->load->helper('url');
//		$this->session->unset_userdata(array('username'=>''));
		$this->session->sess_destroy();
		redirect('/member/login', 'refresh');
	}
	
	/**
	 * 注册
	 */
	public function reg(){				
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username', '用户名 ',  'required|min_length[4]|max_length[12]|callback_username_check');
		$this->form_validation->set_rules('pwd', '密码', 'required');
		$this->form_validation->set_rules('pwd1', '第二个密码', 'required|matches[pwd]');
		$this->form_validation->set_rules('email', '邮箱', 'required|callback_email_check');
		$this->form_validation->set_rules('name', '称呼', 'required');
		
		if($this->form_validation->run() == FALSE){
			$this->load->view('templates/header');
			$this->load->view('member/reg');
			$this->load->view('templates/footer');
		}else{
			
			//写入数据库
			$set_arr = array(
				'username'	 =>$this->input->post('username'),
				'pwd'			=>$this->input->post('pwd')			,
				'email'			=> $this->input->post('email')
			);
			 if($this->db->insert('member', $set_arr) ){
				 $mid = $this->db->insert_id();
				 $other_arr = array(
					 'uid'		=> $mid,
					 'ucode'	=> time(),
				 	 'name'		=> $this->input->post('name')
				 );
				 $this->db->insert('teacher', $other_arr);
				 
				 $userdata = array(
						'uid'	=>$mid,
						'username'=>$set_arr['username']
				);
				$this->session->set_userdata($userdata);

				 $this->load->view('templates/header');
				$this->load->view('templates/success', array('successinfo'=>'注册成功', 'url'=>'/'));
				$this->load->view('templates/footer');
			 }
			 
			
		}
		
		
	}
	public function username_check($str) {
		$dd = $this->member_model->username_check($str);	
		
		if ( $dd['cnt']>0 ) {
			$this->form_validation->set_message ( 'username_check', '  %s   '.$str.'已经被占用' );
			return FALSE;
		} else {
			return TRUE;
		}
	}
	public function email_check($str){
		$d = $this->member_model->username_check($str, 'email');
		if($d['cnt']>0){
			$this->form_validation->set_message('email_check', '%s '.$str.'已经被占用');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	public function teacher($slug=FALSE){
		if($slug != FALSE){
			
		}else{
			$q = $this->member_model->teacher_list();
			if($q->num_rows() > 0){
				foreach ($q->result_array() as $row){
					echo '人名：', $row['name'], '<br />';
				}
			}
//			print_r($q->num_rows());
		}
	}
	
}